package org.siqisource.webstart.core.widgets.tree.service;

import java.util.ArrayList;
import java.util.List;

import org.siqisource.webstart.core.widgets.tree.config.TreeConfig;
import org.siqisource.webstart.core.widgets.tree.constants.TreeConstants;
import org.siqisource.webstart.core.widgets.tree.mapper.TreeMapper;
import org.siqisource.webstart.core.widgets.tree.model.TreeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class TreeDragAndDropService {

	/** 树操作类 */
	@Autowired
	private TreeMapper treeMapper = null;

	/**
	 * 在拖拽放下的时候,获得到目标位置的父节点
	 * 
	 * @param treeConfig
	 *            fieldMapping
	 * @param targetNodeId
	 *            targetNodeId
	 * @param moveType
	 *            moveType
	 * @return TreeNode
	 */
	private TreeNode getParentNodeOnDrop(TreeConfig treeConfig, int targetNodeId, String moveType) {
		TreeNode treeNode = treeMapper.readNode(treeConfig, targetNodeId);
		if (!TreeConstants.MOVE_TYPE_INNER.equals(moveType)) {
			treeNode = treeMapper.readNode(treeConfig, treeNode.getParentId());
		}
		return treeNode;
	}

	/**
	 * 在拖拽放下的时候，获得到目标位置的序号
	 * 
	 * @param treeConfig
	 *            fieldMapping
	 * @param targetNodeId
	 *            targetNodeId
	 * @param moveType
	 *            moveType
	 * @return 目标位置的序号
	 */
	private Integer getSortNoOnDrop(TreeConfig treeConfig, Integer targetNodeId, String moveType) {
		Integer sortNo = 0;
		TreeNode treeNode = treeMapper.readNode(treeConfig, targetNodeId);
		if (TreeConstants.MOVE_TYPE_PREV.equals(moveType)) {
			treeNode = treeMapper.readNodeBefore(treeConfig, treeNode);
		} else if (TreeConstants.MOVE_TYPE_INNER.equals(moveType)) {
			treeNode = treeMapper.readLastChild(treeConfig, targetNodeId);
		}
		if (treeNode != null) {
			sortNo = treeNode.getSortNo();
			sortNo++;
		}
		return sortNo;
	}

	public void dragNodes(TreeConfig treeConfig, List<Integer> nodeIds, Integer targetNodeId, String moveType) {
		// 根据目标节点和移动方式判断SortNo和ParentNode
		Integer sortNo = getSortNoOnDrop(treeConfig, targetNodeId, moveType);

		TreeNode newParentNode = getParentNodeOnDrop(treeConfig, targetNodeId, moveType);
		Integer newParentNodeId = newParentNode.getId();
		String newFullPath = newParentNode.getFullPath();

		// 目标位置的兄弟节点移位
		treeMapper.updateSortNoBatch(treeConfig, newParentNodeId, sortNo, nodeIds.size());

		List<Integer> oldParentsIds = new ArrayList<Integer>(); // 存储被拖拽节点的原始父节点

		// 更新被拖拽的节点
		for (Integer nodeId : nodeIds) {
			TreeNode treeNode = treeMapper.readNode(treeConfig, nodeId);
			String oldFullPath = treeNode.getFullPath();
			// 存储原始父节点
			Integer oldParentId = treeNode.getParentId();
			if (!oldParentsIds.contains(oldParentId)) {
				oldParentsIds.add(oldParentId);
			}

			treeNode.setSortNo(sortNo++);
			treeNode.setFullPath(newFullPath + nodeId + TreeConstants.PATH_SEPARATOR);
			treeNode.setParentId(newParentNodeId);
			treeMapper.updateNode(treeConfig, treeNode);

			// 更新子节点的全路径
			treeMapper.updateOffspringFullPath(treeConfig, oldFullPath, treeNode.getFullPath());
		}

		// 更新旧父节点是否叶子节点状态
		for (Integer oldParentNodeId : oldParentsIds) {
			if (treeMapper.readFirstChild(treeConfig, oldParentNodeId) == null) {
				treeMapper.updateIsLeaf(treeConfig, newParentNodeId, true);
			}
		}

		// 更新新父节点是否叶子节点状态
		if (newParentNode.getIsLeaf()) {
			treeMapper.updateIsLeaf(treeConfig, newParentNodeId, false);
		}
	}
}
